//
//  ConfirmPasswordViewController.m
//  PigMarket
//
//  Created by mac on 2019/5/4.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "ConfirmPasswordViewController.h"
#import "PayPasswordView.h"


@interface ConfirmPasswordViewController ()
/**
 passwordView
 */
@property (nonatomic , strong) PayPasswordView *passwordView;
@end

@implementation ConfirmPasswordViewController

#pragma mark - view life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setUpView];
}


#pragma mark - init method
- (void)setUpView {
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}
- (void)initView {
    [self.view addSubview:self.passwordView];
}

- (void) initLayout {
    
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(TJ_NavBarHeight);
        make.left.right.bottom.mas_equalTo(self.view);
    }];

}

- (void) initAction {
    [self.passwordView.confirmBtn addTarget:self action:@selector(confirmTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.passwordView.forgetPasswordBtn addTarget:self action:@selector(forgetPasswordTapped) forControlEvents:UIControlEventTouchUpInside];
}

- (void)initData {
    self.passwordView.titleLab.text = @"再次输入支付密码";
    [self.passwordView.confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
    
}

#pragma mark - delegate

#pragma mark - target action
- (void)confirmTapped {
    [self resignResponder];
    
    NSString *payPwd = self.passwordView.passwordView.vertificationCode;
    if (payPwd.length != 6) {
        [self toastWithContent:@"请输入六位支付密码"];
        return;
    }
    if (![payPwd isEqualToString:self.payPwd]) {
        [self toastWithContent:@"两次密码输入不一致"];
        return;
    }
    NSNumber *memberId = [UserInfoDataProvider sharedInstance].userInfo.memberId;
    @weakify(self);
    [[[PigMarketApi sharedInstance] modifyPayPasswordWithMemberId:memberId payPwd:payPwd] subscribeNext:^(NSNull * _Nullable x) {
        @strongify(self);
        [self toastWithContent:@"支付密码设置成功"];
        
        @weakify(self);
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            @strongify(self);
            
           // [self.rtNavigationController removeViewControllerWithName:@"PayPasswordViewController"];
            [self.navigationController popViewControllerAnimated:YES];
        });
 
    } error:^(NSError * _Nullable error) {
        @strongify(self);
        [self handlingError:error];
    }];
}
- (void)forgetPasswordTapped {
    
}
#pragma mark - private method
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self resignResponder];
}
- (void)resignResponder {
    for (UITextField *tf in self.passwordView.passwordView.textFieldArray) {
        
        [tf resignFirstResponder];
    }
}
#pragma mark - getter and setter
- (PayPasswordView *)passwordView {
    if (!_passwordView) {
        _passwordView = [[PayPasswordView alloc] init];
        _passwordView.backgroundColor = [UIColor whiteColor];
    }
    return _passwordView;
}

@end

